<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0">
      <div v-show="scene === 0">
        <el-button
          @click="addSpu"
          type="primary"
          icon="Plus"
          :disabled="!categoryStore.c3Id"
        >
          添加SPU
        </el-button>
        <!-- 展示已有SPU数据 -->
        <el-table style="margin: 10px 0" border :data="records">
          <el-table-column
            align="center"
            label="序号"
            type="index"
            width="80ox"
          ></el-table-column>
          <el-table-column
            align="center"
            label="SPU名称"
            width="120px"
            prop="spuName"
          ></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="center" label="SPU操作">
            <template #default="{ row }">
              <el-button
                type="primary"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
                circle
              ></el-button>
              <el-button
                type="primary"
                icon="Edit"
                title="修改SPU"
                @click="updateSpu(row)"
                circle
              ></el-button>
              <el-button
                type="primary"
                icon="View"
                title="查看SKU列表"
                @click="findSku(row)"
                circle
              ></el-button>
              <el-button
                type="primary"
                icon="Delete"
                title="删除SPU"
                @click="deleteSpu(row)"
                circle
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper, ->,sizes, total,"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </div>
      <!-- 添加SPU|修改SPU子组件 -->
      <spuForm
        ref="spu"
        v-show="scene === 1"
        @changeScene="changeScene"
      ></spuForm>
      <!-- 添加sku子组件 -->
      <skuForm
        ref="sku"
        v-show="scene === 2"
        @changeScene="changeScene"
      ></skuForm>
      <!-- dialog对话框：展示已有的SKU数据 -->
      <el-dialog v-model="show" title="SKU列表">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #default="{ row }">
              <img
                :src="row.skuDefaultImg"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue'
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu'
import type {
  HasSpuResponseData,
  Records,
  SpuData,
  SkuData,
  SkuInfoData,
} from '@/api/product/spu/type'
// 引入分类的仓库
import useCategoryStore from '@/store/modules/category'
const categoryStore = useCategoryStore()
// 引入子组件
import skuForm from './components/skuForm.vue'
import spuForm from './components/spuForm.vue'
// 场景的数据 0：显示已有SPU数据 1：添加SPU 2：添加SKU
const scene = ref(0)
// 分页器默认页码
const pageNo = ref(1)
// 每一页展示几条数据
const pageSize = ref(3)
// 存储已有的SPU数据
const records = ref<Records>([])
// 存储已有SPU总个数
const total = ref(0)
// 存储全部SKU的数据
const skuArr = ref<SkuData[]>([])
// 控制对话框的显示与隐藏
const show = ref(false)
// 获取子组件实例SpuForm
const spu = ref()
// 获取子组件实例SkuForm
const sku = ref()

// 监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    // 务必保证有三级分类ID，否则会报错
    if (!categoryStore.c3Id) return
    getHasSpu()
  },
)

// 此方法执行：可以获取某一个三级分类下的全部已有SPU
const getHasSpu = async (pager = 1) => {
  // 修改当前页码
  pageNo.value = pager
  const res: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (res.code === 200) {
    records.value = res.data.records
    total.value = res.data.total
  }
}

// 分页器下拉菜单发生变化的时候生效
const changeSize = () => {
  getHasSpu()
}

// 添加新的SPU按钮的回调
const addSpu = () => {
  // 切换为场景1：添加与修改已有的SPU结果 -> spuForm
  scene.value = 1
  // 点击添加SPU按钮，调用子组件的方法初始化数据
  spu.value.initAddSpu(categoryStore.c3Id)
}

// 修改已有SPU按钮的回调
const updateSpu = (row: SpuData) => {
  // 切换为场景1：添加与修改已有的SPU结果 -> spuForm
  scene.value = 1
  // 调用子组件实例方法获取完整已有的SPU数据
  spu.value.initHasSpuData(row)
}

// 子组件spuForm绑定自定义事件：目前是让子组件通知父组件切换场景为0
const changeScene = (obj: any) => {
  // 子组件spuForm点击取消变为场景0：展示已SPU
  scene.value = obj.flag
  // 再次获取全部的已有SPU
  if (obj.params === 'update') {
    // 更新留在当前页
    getHasSpu(pageNo.value)
  } else {
    // 添加留在第一页
    getHasSpu()
  }
}

// 添加SKU按钮的回调
const addSku = (row: SpuData) => {
  // 点击添加SKU按钮切换场景为2
  scene.value = 2
  // 调用子组件的方法初始化添加SKU数据
  sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}

// 查看SKU列表的数据
const findSku = async (row: SpuData) => {
  const res: SkuInfoData = await reqSkuList(row.id as number)
  if (res.code === 200) {
    skuArr.value = res.data
    // 对话框显示
    show.value = true
  }
}

// 删除已有的SPU按钮的回调
const deleteSpu = async (row: SpuData) => {
  const res: any = await reqRemoveSpu(row.id as number)
  if (res.code === 200) {
    ElMessage.success('删除成功')
    // 重新获取已有的SPU数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage.error('删除失败')
  }
}

// 路由组件销毁的时候，把仓库分类相关数据清空
onBeforeUnmount(() => {
  // 清空仓库的数据
  categoryStore.clear()
})
</script>

<style scoped></style>
